//
//  VisitorLoginView.swift
//  云Sir的微博
//
//  Created by 赵云 on 15/10/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

 /// 访客视图代理
protocol VisitorLoginViewDelegate: NSObjectProtocol{
    func visitorLoginViewWillRegister()
    func visitorLoginViewWillLogin()
}

class VisitorLoginView: UIView {
    
    /// 定义代理 -- 要用weak
   weak var delegate: VisitorLoginViewDelegate?
    
    /// 按钮的监听方法
    func clickRegisterBtn(){
        delegate?.visitorLoginViewWillRegister()
    }
    
    func clickLoginBtn(){
        delegate?.visitorLoginViewWillLogin()
    }
    
    //MARK: - 设置视图信息
    func setupIconInfo(ishome: Bool, imageName: String, message: String){
        
        messageLabel.text = message
        iconView.image = UIImage(named: imageName)
        
        
        homeIconView.hidden = !ishome

        
        ishome ? startAnimation() : sendSubviewToBack(maskIconView)
        
        
        
    }
    //设置开始动画
    private func startAnimation(){
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.toValue = 2 * M_PI
        ani.repeatCount = MAXFLOAT
        ani.duration = 20.0
        //防止切换界面动画停止
        ani.removedOnCompletion = false
        
        //把动画添加到视图的图层中
        iconView.layer.addAnimation(ani, forKey: nil)
        
    }
    
    
    //界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    //设置界面
    private func setupUI() {
        
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //自动布局
        //自动布局需要设置一个属性
        //设置图标的位置
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        //设置房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //消息文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
       addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        //注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //登陆按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //遮罩视图  VFL可视化格式语言
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView,"regButton": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
    //MARK: - 懒加载控件
    /// 添加图标
    private lazy var iconView: UIImageView = {
        
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iv
        
    }()
    
    /// 添加遮罩视图
    private lazy var maskIconView: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        
        }()
    
    /// 添加房子
    private lazy var homeIconView: UIImageView = {
        
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return hv
        
    }()
    
    /// 添加消息
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        label.numberOfLines = 0
        
        label.sizeToFit()
        
        return label
        
    }()
    
    /// 注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置按钮的点击方法
        btn.addTarget(self, action: "clickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    /// 登陆按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        //设置按钮的点击事件
        btn.addTarget(self, action: "clickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
        
    }()
    
   
}
